package com.electric.service.impl;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.electric.common.Contains;
import com.electric.common.LoggerInterface;
import com.electric.entity.RepairType;
import com.electric.mapper.RepairTypeMapper;
import com.electric.service.IRepairTypeService;

/**
 * <p>
 * 故障类型 服务实现类
 * </p>
 *
 * @author zhangbin
 * @since 2018-08-28
 */
@Service
public class RepairTypeServiceImpl extends ServiceImpl<RepairTypeMapper, RepairType> implements IRepairTypeService,LoggerInterface {
	
	public Page<RepairType> selectPage(int pageNum,int pageSize, String keyword) {
		EntityWrapper<RepairType> searchWrapper = new EntityWrapper<RepairType>();
		if(StringUtils.isNotBlank(keyword)) {
			searchWrapper.like("repair_type", keyword);
		}
		searchWrapper.orderBy("repair_type", true);
		
		Page<RepairType> page = new Page<RepairType>(pageNum, pageSize);
		
		page = selectPage(page, searchWrapper);
		
		return page;
	}

	@Override
	public boolean loadRepairTypeToContainsMap() {
		try{
			EntityWrapper<RepairType> searchWrapper = new EntityWrapper<RepairType>();
			
			List<RepairType> list = selectList(searchWrapper);
			
			// 打印读取结果
			logger.debug(list);
			
			// 清空map
			logger.debug(Contains.REPAIR_TYPE_ID_CODE);
			logger.debug(Contains.REPAIR_TYPE_CODE_ID);
			Contains.REPAIR_TYPE_ID_CODE.clear();
			
			Iterator<RepairType> iterator = list.iterator();
			
			while(iterator.hasNext()){
				RepairType next = iterator.next();
				Contains.REPAIR_TYPE_ID_CODE.put(next.getSeqId(), next.getCode());
				
				Contains.REPAIR_TYPE_CODE_ID.put(next.getCode(), next.getSeqId());
			}
			logger.debug(Contains.REPAIR_TYPE_ID_CODE);
			logger.debug(Contains.REPAIR_TYPE_CODE_ID);
			
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return false;
		}
	}

	@Override
	public List<RepairType> selectAllRepairType() {
		EntityWrapper<RepairType> searchWrapper = new EntityWrapper<RepairType>();
		searchWrapper.orderBy("repair_type", true);
		
		return selectList(searchWrapper);
	}
}
